package controller

import (
	"hft/Database/admin"
	services "hft/Http/Services"
	utils "hft/Http/Utils"
	"hft/tools"
	"strconv"

	"github.com/gin-gonic/gin"
)

type CitySetting struct{}

func (*CitySetting) GetManageCities(c *gin.Context) {
	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	data, err := services.CitySettingService.GetManageCities(claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)
}

func (*CitySetting) GetCityConfig(c *gin.Context) {

	id, _ := strconv.Atoi(c.DefaultQuery("id", "0"))

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	data, err := services.CitySettingService.GetCityConfig(claims.BaseClaims, id)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)
}

func (*CitySetting) EditCityConfig(c *gin.Context) {
	var data *admin.CitySetting

	c.ShouldBindJSON(&data)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.CitySettingService.EditCityConfig(data, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

var CitySettingController = new(CitySetting)
